package com.fjnu.dao.impl;

import java.awt.SystemColor;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Query;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Repository;

import com.fjnu.dao.ImainInstrumentDao;
import com.fjnu.dao.common.BaseDao;
import com.fjnu.dao.common.BaseDaoImpl;
import com.fjnu.dao.common.DaoException;
import com.fjnu.entities.MainInstrument;
@Repository("mainInstrumentDao")
public class mainInstrumentDao extends BaseDaoImpl<MainInstrument> implements ImainInstrumentDao {

	//private static final String mainSelect = "select instrumentNum,instrumentName,model,storageAreaName,statusQuo ";
	@Override
	public void addInstrument(MainInstrument maininstrument) {
		save(maininstrument);

	}

	@Override
	public void delInstrument(int id) {
		//String hql = "delete mainInstrument m where m.id=?";
		//Query query = getSession().createQuery(hql);
		//query.setInteger(0, id);
		//return (query.executeUpdate()>0);
		delete(id);
	}

	@Override
	public List<MainInstrument> findManyInstrument(String condition, int pageSize, int pageIndex) {
		String hql ="from MainInstrument";
		//hql+="limit ?,?";
		//查找框根据查询条件查询数据
		if(condition!=null && !"".equals(condition)){
			hql+=" where instrumentName like '%"+condition+"%' or storageAreaName like '%"+condition+"%' or instrumentNum like '%"+condition+"%'";
		}
		Query query = getSession().createQuery(hql);
		query.setFirstResult((pageIndex-1)*pageSize);
	    query.setMaxResults(pageSize);
	    //List list = find(pageIndex,pageSize,hql);
	    if(query.list().size()==0){
	    	throw new DaoException("找不到所需的主机设备");
	    }
		return query.list();
	}
	//根据仪器编号快速查询
	public List<MainInstrument> findInstrumentNum(Integer page,Integer count){
		 String hql="from MainInstrument m order by m.instrumentNum";
		 List<MainInstrument> list = queryPage(hql,page,count);
		 return list;
		 
	}
	//根据领用单位号快速查询
	public List<MainInstrument> findRecipientsUnitNum(Integer page,Integer count){
		 String hql="from MainInstrument m order by m.recipientsUnitNum";
		 List<MainInstrument> list = queryPage(hql,page,count);
		 return list;
		 
	}
	//根据仪器分类号快速查询
	public List<MainInstrument> findclassification(Integer page,Integer count){
		 String hql="from MainInstrument m order by m.classification";
		 List<MainInstrument> list = queryPage(hql,page,count);
		 return list;
			 
	}
		
/**
 * 对主机设备进行一般修改
 */
	@Override
	public void updateInstrument(MainInstrument maininstrument) {
		//String[] names={};
		//partUpdate(maininstrument.getId(),names);
		update(maininstrument);
	}

/**
 * 修改领用人
 */
	public void updateRecipientsPerson(int id,String recipientsPerson) {
		String[] names={recipientsPerson};
		partUpdate(id, names,recipientsPerson);
	
	}

}
